package cn.fl.project.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.condition.PrjTransferInfoQC;
import cn.fl.project.transfer.service.IPrjTransferInfoService;
import cn.fl.project.transfer.vo.PrjTransferInfoVO;

/**
 * @Description: 项目移交记录 控制层
 */
@Controller
@RequestMapping("/project/prjtransferinfo")
public class PrjTransferInfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(PrjTransferInfoController.class);
	
	@Autowired
	private IPrjTransferInfoService prjTransferInfoService;
	/**
	 * 客户 服务层BEAN
	 */
	@Autowired
	private ICstBscInfoService cstBscInfoService;//客户基础信息表
	

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("project/prjbscinfo/prjTransferInfoList");
		// 事业部
		List<KeyValueVO> orgList = this.cstBscInfoService.searchBusinessOrgList();
		String currentOrgCd = CurrentThreadContext.getCurrentOrgCd();
		if(currentOrgCd.length()>8){
			if(CollectionUtils.isNotEmpty(orgList)){
				for(KeyValueVO kv:orgList){
					if(kv.getKey().equals(currentOrgCd.substring(0, 8))){
						view.addObject("currOrg", CurrentThreadContext.getCurrentOrgCd());
						break;
					}
				}
			}
		}
		BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String prjNm = request.getParameter("prjNm");
		String contractNo = request.getParameter("contractNo");
		String crtUsrNm = request.getParameter("crtUsrNm");
		String crtTmStart = request.getParameter("crtTmStart");
		String crtTmEnd = request.getParameter("crtTmEnd");
		String cstMgrNm = request.getParameter("cstMgrNm");
		String oldCstMgrNm = request.getParameter("oldCstMgrNm");
		String cstMgrOrgCd = request.getParameter("cstMgrOrgCd");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(currentPage)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		PrjTransferInfoQC qc = new PrjTransferInfoQC();
		if (StringUtils.isNotEmpty(prjNm)) {
			qc.setPrjNm(prjNm);
		}
		if(StringUtils.isNotBlank(contractNo)){
			qc.setContractNo(contractNo);
		}
		if(StringUtils.isNotEmpty(crtUsrNm)){
			qc.setCrtUsrNm(crtUsrNm);
		}
		if (StringUtils.isNotEmpty(crtTmStart)) {
			qc.setCrtTmStart(crtTmStart);
		}
		if(StringUtils.isNotBlank(crtTmEnd)){
			qc.setCrtTmEnd(crtTmEnd);
		}
		if(StringUtils.isNotBlank(cstMgrNm)){
			qc.setCrtUsrNm(crtUsrNm);
		}
		if(StringUtils.isNotBlank(oldCstMgrNm)){
			qc.setOldCstMgrNm(oldCstMgrNm);
		}
		if(StringUtils.isNotBlank(cstMgrOrgCd)){
			qc.setCstMgrOrgCd(cstMgrOrgCd);
		}
		if(StringUtils.isNotBlank(cstMgrNm)){
			qc.setCstMgrNm(cstMgrNm);
		}
		
		//添加权限
		qc.setUsrId(CurrentThreadContext.getCurrentUserId());	
		page.setCondition(qc);

		Pager pager = this.prjTransferInfoService.searchListPage(page);
		ListPageVO<PrjTransferInfoVO> listPage = new ListPageVO<PrjTransferInfoVO>((List<PrjTransferInfoVO>) pager.getResultList(), pager.getTotalCount());
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}
	
	
	
	
	
}

